
#include"../mqserver/mq_binding.hpp"
#include<gtest/gtest.h>


xiang11mq::BindingManager::ptr bmp;

class BingdingTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        std::cout << "单元测试执行前的环境初始化" << std::endl;
        bmp = std::make_shared<xiang11mq::BindingManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        std::cout << "单元测试执行完毕后的环境清理" << std::endl;
        //bmp->clear();
    }
};

TEST(binding_test, insert_test)
{
    bmp->bind("exchange1", "queue1", "key1", true);
    bmp->bind("exchange1", "queue2", "key2", true);
    bmp->bind("exchange1", "queue3", "key5", true);
    bmp->bind("exchange2", "queue1", "key3", true);
    bmp->bind("exchange2", "queue2", "key4", true);
    bmp->bind("exchange2", "queue3", "key", true);
    ASSERT_EQ(bmp->size(), 6);
}

TEST(binding_test, select_test) 
{
    ASSERT_EQ(bmp->exists("exchange1", "queue1"), true);
    ASSERT_EQ(bmp->exists("exchange1", "queue2"), true);
    ASSERT_EQ(bmp->exists("exchange1", "queue3"), true);
    ASSERT_EQ(bmp->exists("exchange2", "queue1"), true);
    ASSERT_EQ(bmp->exists("exchange2", "queue2"), true);
    ASSERT_EQ(bmp->exists("exchange2", "queue3"), true);

    xiang11mq::Binding::ptr bp = bmp->getBinding("exchange1", "queue1");
    ASSERT_NE(bp.get(), nullptr);
    ASSERT_EQ(bp->exchange_name, std::string("exchange1"));
    ASSERT_EQ(bp->msgqueue_name, std::string("queue1"));
    ASSERT_EQ(bp->binding_key, std::string("key1"));
}

TEST(binding_test, select_exchange_test) 
{
    xiang11mq::MsgQueueBindingMap mpbm = bmp->getExchangeBingdings("exchange1");
    ASSERT_EQ(mpbm.size(), 3);
    ASSERT_NE(mpbm.find("queue1"), mpbm.end());
    ASSERT_NE(mpbm.find("queue2"), mpbm.end());
}

//e1-q1
//e1-q2
//e1-q3
//e2-q1
//e2-q2
//e2-q3

TEST(binding_test, remove_queue_test) 
{
    bmp->removeMsgQueueBindings("queue1");
    ASSERT_EQ(bmp->exists("exchange1", "queue1"), false);
    ASSERT_EQ(bmp->exists("exchange2", "queue1"), false);
}

//e1-q2
//e1-q3

//e2-q2
//e2-q3

TEST(binding_test, remove_exchange_test) 
{
    bmp->removeExchangeBindings("exchange1");
    ASSERT_EQ(bmp->exists("exchange1", "queue1"), false);
    ASSERT_EQ(bmp->exists("exchange1", "queue2"), false);
    ASSERT_EQ(bmp->exists("exchange1", "queue3"), false);
}

//e2-q2
//e2-q3

TEST(binding_test, remove_single_test) 
{
    ASSERT_EQ(bmp->exists("exchange2", "queue2"), true);
    bmp->unbind("exchange2", "queue2");
    ASSERT_EQ(bmp->exists("exchange2", "queue2"), false);
    ASSERT_EQ(bmp->exists("exchange2", "queue3"), true);
}

//e2-q3

TEST(binding_test, recovery_test) 
{
    ASSERT_EQ(bmp->exists("exchange2", "queue3"), true);
}


int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);

    testing::AddGlobalTestEnvironment(new BingdingTest);
    //testing::AddGlobalTestEnvironment(new MyMapTest);
    int ret = RUN_ALL_TESTS();
    return 0;
}


